package com.freedy.concurrent.Monitors.patterns;

import lombok.extern.slf4j.Slf4j;

/**
 * 保护性暂停
 * @author Freedy
 * @date 2021/4/22 9:31
 */
@Slf4j
public class GuardedSuspension {
    public static void main(String[] args) {
        Guarded guarded = new Guarded();
        //线程1等待线程2的下载结果
        new Thread(()->{
            log.info("等待结果");
            Object o = guarded.get(10000);
            log.info(String.valueOf(o));
        },"t1").start();

        new Thread(()->{
            //模拟下载操作
            for (int i = 0; i < 5; i++) {
                try {
                    log.info(i+"\t正在下载......");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                guarded.complete(i==4?"ok！！！！！":null);
            }
        },"t2").start();
    }
}

